<?php

/**
 * 
 * @author RiderSx
 *
 */
class xineUsersStorageMysql {
	private $querysCount=0, $uniqueColumn;
	
	public function __construct ($connectionString=false, $uniqueColumn='name') {
		if($connectionString) {
			$data = explode(':', $connectionString);
			mysql_connect($data[0], $data[1], $data[2]);
	        mysql_select_db($data[3]);
	        mysql_set_charset('utf8');
		}
		$this->uniqueColumn = $uniqueColumn;
	}
	
	/**
	 * Get all information about user by $identifier
	 * 
	 * @param $identifier
	 * @return unknown_type
	 */
	public function getUser ($identifier) {
		$result = $this->query("SELECT * FROM `xine_users` WHERE `$this->uniqueColumn`='$identifier' LIMIT 1");
		
		return $result?mysql_fetch_assoc($result):false;
	}
	
	/**
	 * Register a new user
	 * Method will respond false if something wrong or a new user id
	 * 
	 * 
	 * @param $data
	 * @return int 
	 */
	public function setUser ($data=array()) {
		$result = $this->query("INSERT INTO `xine_users` SET
			`name`='{$data['name']}',
			`email`='{$data['email']}',
			`password`='{$data['password']}',
			`public_name`='{$data['public_name']}',
			`discussions_count`='0',
			`replys_count`='0',
			`registration_time`='{$data['time']}',
			`openid_identity`='{$data['openid_identity']}',
			`openid_provider`='{$data['openid_provider']}',
			`group`='1',
			`popularity`='0'
		");
		
		return $result?mysql_insert_id():false;
	}
	
	/**
	 * 
	 * @param $identifier
	 * @param $data
	 * @return unknown_type
	 */
	public function updateUser ($identifier, $data=array()) {
		$result = $this->query("UPDATE `xine_users` SET
			".  ($data['public_name']?'`public_name`=\''.$data['public_name'].'\', ':'').
				($data['email']?'`email`=\''.$data['email'].'\', ':'').
				($data['password']?'`password`=\''.$data['password'].'\', ':'').
				($data['discussions_count']?'`discussions_count`=\''.$data['discussions_count'].'\', ':'').
				($data['replys_count']?'`replys_count`=\''.$data['replys_count'].'\', ':'').
				($data['group']?'`group`=\''.$data['group'].'\', ':'').
				($data['popularity']?'`popularity`=\''.$data['popularity'].'\', ':'')
			."
			WHERE `$this->uniqueColumn`='{$identifier}'
		");
			
			return $result?true:false;
	}
	
	public function checkUser ($identifier, $password) {
		$result = $this->query("SELECT `group` FROM `xine_users` WHERE `$this->uniqueColumn`='$identifier' AND `password`='$password' LIMIT 1");
		
		if(mysql_num_rows($result) == 1) return true;
		else return false;
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	public function getACL () {
		$result = $this->query("SELECT * FROM `xine_acl`");
		
		$return = array();
		
		while($acs=mysql_fetch_assoc($result)) {
			$return[] = $acs;
		}
		
		return $return;
	}
	
	/**
	 * 
	 * @param $queryString
	 * @return unknown_type
	 */
	private function query ($queryString) {
		$resource = mysql_query($queryString);
		
		if($resource) {
			$this->querysCount++;
			return $resource;
		} else {
			return false;
		}
	}
}